package com.neusoft.yuejian_01.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.neusoft.yuejian_01.R;
import com.neusoft.yuejian_01.adapter.GiftListAdapter;
import com.neusoft.yuejian_01.javabean.GiftBean;
import com.neusoft.yuejian_01.listener.RetrofitListener;
import com.neusoft.yuejian_01.model.GiftModel;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class GiftFragment extends Fragment implements RetrofitListener{
    private RecyclerView recyclerView;
    private  View view=null;
    private   MeFragment meFragment;
    private ImageView back;
    private FragmentManager manager;
    public GiftFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.fragment_gift,container,false);
        recyclerView = (RecyclerView) view.findViewById(R.id.giftlist);
        back= (ImageView) view.findViewById(R.id.gback);
        recyclerView.setLayoutManager(new GridLayoutManager(this.getActivity(),3));
        initData();
        initEvents();
        return view;
    }
//返回
    private void initEvents() {
        back.setOnClickListener(new View.OnClickListener() {
            @Override

            public void onClick(View view) {
                meFragment = new MeFragment();
                replace( meFragment);
            }
        });

    }
//获取礼物的列表
    private void initData() {
      GiftModel giftModel= new GiftModel();
      giftModel.getGiftlist(this);
    }
    //fragment跳转
    private void replace(Fragment fragment) {
        manager = getChildFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.Gift, fragment);
        transaction.commit();
    }

    @Override
    public void onSuccess(Object object, int flag) {
        List<GiftBean> giftBean =(List<GiftBean>)object;
        recyclerView.setAdapter(new GiftListAdapter(getActivity(),giftBean));
    }

    @Override
    public void onFail() {
    }
}
